package com.cloudstore.eccom.gyxy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.eccom.gyxy.utils.MessageUtil;
import com.weaver.base.msgcenter.channel.IMessageReceive;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.gyxy.rabbitmq.util.SendMessagesUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author dusongsong
 * @date 2023年09月19日 11:40
 */
public class WorkflowMessage extends BaseBean implements IMessageReceive {
    @Override
    public void onMessage(String paramString) {
        writeLog(">>>>>WorkflowMessage>>>>>paramString=" + paramString);
        List<MessageBean> localList = JSONArray.parseArray(paramString, MessageBean.class);
        try {
            ResourceComInfo comInfo = new ResourceComInfo();
            if (localList != null && !localList.isEmpty()) {
                for (MessageBean messageBean : localList) {
                    int code = messageBean.getMessageType().getCode();
                    String detailBaseName = Util.null2String(messageBean.getParams().get("detailBaseName"));
                    String operatorId = Util.null2String(messageBean.getParams().get("operatorId"));
                    String datetime = Util.null2String(messageBean.getParams().get("date"));
                    String receiveDate = Util.null2String(messageBean.getParams().get("receiveDate"));
                    String operaterDate = Util.null2String(messageBean.getParams().get("operaterDate"));
                    String sender = comInfo.getLastname(messageBean.getCreater() + "");
                    String requestid = messageBean.getTargetId();
                    String title = messageBean.getTitle();
                    Set<String> userList = messageBean.getUserList();
                    writeLog(">>>>>WorkflowMessage>>>>>getCode=" + code);
                    if (code == 19 || code == 20 || code == 21 || code == 22 || code == 23 || code == 24) {
                        String tokenUrl = "http://58.193.84.162:82/api/rest/token/getAccessToken";
                        String param = "appkey=u7WydyxyKIjR4ybAab&appsecret=1cd2038c27fc48fe83e12d39a9075e0e";
                        String result = MessageUtil.getToken(tokenUrl, param);
                        for (String userid : userList) {
                            JSONObject nodebase = MessageUtil.getNodeBase(requestid);
                            String loginId = comInfo.getLoginID(userid);
                            String pcurl = "http://xtbg.wxgyxy.edu.cn/login/ssoTogyxy.jsp?loginid=" + loginId + "&requestid=" + requestid;
                            String emurl = "http://ydoa.wxgyxy.edu.cn:9000/login/ssoTogyxyEM.jsp?loginid=" + loginId + "&requestid=" + requestid;
                            writeLog(">>>>>WorkflowMessage>>>>>loginid=" + loginId + ",requestid=" + requestid);
                            String sendUrl = "http://58.193.84.162:82/api/rest/message/sendMessage";
                            if (!"".equals(result)) {
                                //推送微信公众号消息
                                JSONObject resultJson = JSONObject.parseObject(result);
                                String accessToken = resultJson.getString("access_token");
                                Map<String, String> requestText = new HashMap<String, String>();
                                requestText.put("accesstoken", URLEncoder.encode(accessToken, "UTF-8"));
                                requestText.put("xxlx", URLEncoder.encode("wxgzh", "UTF-8"));
                                requestText.put("userid", URLEncoder.encode(loginId, "UTF-8"));
                                requestText.put("sender", URLEncoder.encode(sender, "UTF-8"));
                                requestText.put("msgtype", "1");
                                requestText.put("title", URLEncoder.encode(title, "UTF-8"));
                                requestText.put("templatecode", URLEncoder.encode("OADBTZ", "UTF-8"));
                                requestText.put("templateparam", URLEncoder.encode("{'sxmc':'" + detailBaseName + "','sqr':'" + sender + "','blhj':'" + nodebase.getString("nodename") + "'}", "UTF-8"));
                                requestText.put("url", URLEncoder.encode(emurl, "UTF-8"));
                                requestText.put("sfrz", "0");
                                writeLog(">>>>>WorkflowMessage>>>>>requestText=" + requestText);
                                String messageResult = MessageUtil.sendRequest(sendUrl, requestText, null);
                                writeLog(">>>>>WorkflowMessage>>>>>messageResult=" + messageResult);
                                //推送短信消息
                                Map<String, String> requestSMSText = new HashMap<String, String>();
                                requestSMSText.put("accesstoken", URLEncoder.encode(accessToken, "UTF-8"));
                                requestSMSText.put("xxlx", URLEncoder.encode("sms", "UTF-8"));
                                requestSMSText.put("userid", URLEncoder.encode(loginId, "UTF-8"));
                                requestSMSText.put("sender", URLEncoder.encode(sender, "UTF-8"));
                                requestSMSText.put("msgtype", "4");
                                requestSMSText.put("title", URLEncoder.encode(title, "UTF-8"));
                                requestSMSText.put("templatecode", URLEncoder.encode("OADBTZ", "UTF-8"));
                                requestSMSText.put("templateparam", URLEncoder.encode("{'sxmc':'" + detailBaseName + "','sqr':'" + sender + "','blhj':'" + nodebase.getString("nodename") + "'}", "UTF-8"));
                                requestSMSText.put("url", URLEncoder.encode(emurl, "UTF-8"));
                                requestSMSText.put("sfrz", "0");
                                writeLog(">>>>>WorkflowMessage>>>>>requestSMSText=" + requestSMSText);
                                String smsResult = MessageUtil.sendRequest(sendUrl, requestSMSText, null);
                                writeLog(">>>>>WorkflowMessage>>>>>smsResult=" + smsResult);
                                //推送门户消息
                                SendMessagesUtil sendMessagesUtil = new SendMessagesUtil();
                                //发送流程任务
                                //流程ID （每条流程唯一的ID）
                                String lcid = requestid+userid + "oa";
                                //节点ID（当前流程的每个节点唯一的ID）
                                String jdid = nodebase.getString("nodeid");
                                //标题说明（当前任务的标题说明）
                                String btsm = nodebase.getString("requestname");
                                //办理地址 （填写相对地址，地址前加/）
                                String bldz = pcurl;
                                //流程跟踪地址 （填写相对地址，地址前加/）
                                String lcgzdz = pcurl;
                                //当前节点 （当前流程的每个节点唯一的名称）
                                String dqjd = nodebase.getString("nodename");
                                //使用角色 （当前审批人使用的角色）
                                String syjs = comInfo.getLoginID(userid);
                                //查阅状态 （1:已阅 2：未阅）
                                String cyzt = "1";
                                //任务状态
                                String status = "1";
                                //任务结果
                                String result1 = "1";
                                //是否有手机表单（1：有  2：没有）
                                String hasmobile = "1";
                                //手机办理地址 （填写相对地址，地址前加/）
                                String mobileurl = emurl;
                                //任务处理时间 （格式：yyyy-MM-dd HH:mm:ss）
                                String clsj = datetime;
                                //审判人
                                String[] sprs = new String[]{loginId};
                                String taskMessage = sendMessagesUtil.sendProcessTask(lcid, jdid, btsm, bldz, sprs, lcgzdz, dqjd, syjs, cyzt, status, result1, hasmobile, mobileurl, clsj);
                                writeLog(">>>>>WorkflowMessage>>>>>taskMessage=" + taskMessage);
                                //发送流程实例
                                //事项ID （每个事项唯一的ID）
                                String sxid = requestid+userid;
                                //事项名称
                                String sxmc = title;
                                //标题说明
                                //申请时间 （格式：yyyy-MM-dd HH:mm:ss）
                                String sqsj = datetime;
                                //持续时间
                                String cxsj = MessageUtil.getDayHourMinute(receiveDate, operaterDate);
                                //申请人（教工号或学号）
                                String sqr = loginId;
                                //来源（与routingKey后缀一致）
                                String source = "oa";
                                //办结时间 （格式：yyyy-MM-dd HH:mm:ss）
                                String bjsj = datetime;
                                String instanceMessage = sendMessagesUtil.sendProcessInstance(lcid, sxid, sxmc, btsm, sqsj, cxsj, sqr, pcurl, source, status, result1, hasmobile, mobileurl, bjsj);
                                writeLog(">>>>>WorkflowMessage>>>>>instanceMessage=" + instanceMessage);
                            } else {
                                writeLog(">>>>>WorkflowMessage>>>>>getToken=" + result);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        writeLog(">>>>>WorkflowMessage>>>>>onStart");
    }

    @Override
    public void onEnd() {
        writeLog(">>>>>WorkflowMessage>>>>>onEnd");
    }

    @Override
    public void setProp(Map<String, String> map) {

    }

    @Override
    public void setList(List<IMessageReceive> list) {

    }
}
